package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.naming.Name;
import java.util.List;
//@CrossOrigin //前端解决跨域了
@RestController
@Tag(name = "轮播图接口管理")
@RequestMapping("/api/banners")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有轮播图信息")
    @GetMapping("/list")
    public Result list(){
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //按照升序排序
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        //调用BannerService中的list方法
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取所有已激活的轮播图信息")
    @GetMapping("/active")
    public Result activeList(){
        //调用bannerService中list方法
        List<Banner> activeList = bannerService.list(new LambdaQueryWrapper<Banner>().eq(Banner::getIsActive,true));
        return Result.success(activeList);
    }

    @Operation(summary = "根据id获取轮播图信息")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

    @Operation(summary = "添加轮播图信息")
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner){
        //调用BannerService中的save方法
        boolean save = bannerService.save(banner);
        if(save){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }

    @Operation(summary = "更新轮播图信息")
    @PutMapping("/update")
    public Result update(@RequestBody Banner banner){
        //调用BannerService中的updateById方法
        boolean update = bannerService.updateById(banner);
        if(update){
            return Result.success("更新成功");
        }else{
            return Result.error("更新失败");
        }
    }

    @Operation(summary = "根据id删除轮播图信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        boolean b = bannerService.removeById(id);
        if (b)  {
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }


























    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file){
        //调用fileuploadservice中uploadFile方法
        String url = fileUploadService.uploadFile(file);
        return Result.success(url,"上传轮播图成功");

    }






}
